package com.example.bruger.androidneurosky;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import java.nio.DoubleBuffer;


/**
 * Created by Ailain on 07-04-2015.
 */
public class MainMenuActivity extends ActionBarActivity implements TGCallBack{
    TGHandler handler;
    boolean connected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        connected = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainmenu);
        handler = new TGHandler();
        Button conBtn = (Button) findViewById(R.id.btnConnect);

        Button btnBrightPictureGame = (Button) findViewById(R.id.btnBrightPictureGame);
        Button btnCatScaleGame = (Button) findViewById(R.id.btnCatScaleGame);
        Button btnMedAttGame = (Button) findViewById(R.id.btnMedAttGame);
        Button btnTraficLight = (Button) findViewById(R.id.btnTraficLight);
        Button btnHighscore = (Button) findViewById(R.id.btnHighscore);
        Button btnRawData = (Button) findViewById(R.id.btnRawData);
        btnBrightPictureGame.setEnabled(false);
        btnCatScaleGame.setEnabled(false);
        //btnHighscore.setEnabled(false);
        btnMedAttGame.setEnabled(false);
        btnTraficLight.setEnabled(false);
        btnRawData.setEnabled(false);

        conBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartTGDevice();
            }
        });

        btnBrightPictureGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToBrigthPictureGame();
            }
        });

        btnCatScaleGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToCatScaleGame();
            }
        });

        btnHighscore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToHighscore();
            }
        });

        btnMedAttGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToMedAttGame();
            }
        });

        btnTraficLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToTraficLightGame();
            }
        });

        btnRawData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeActivityToRawData();
            }
        });

    }

    public void changeActivityToBrigthPictureGame()
    {
        Intent intent = new Intent(this, BrightPictureGameActivity.class);
        startActivity(intent);
    }

    public void changeActivityToCatScaleGame()
    {
        Intent intent = new Intent(this, CatScaleGameActivity.class);
        startActivity(intent);
    }

    public void changeActivityToMedAttGame()
    {
        Intent intent = new Intent(this, MedAttGameActivity.class);
        startActivity(intent);
    }

    public void changeActivityToTraficLightGame()
    {
        Intent intent = new Intent(this, TrafficlightActivity.class);
        startActivity(intent);
    }

    public void changeActivityToHighscore()
    {
        Intent intent = new Intent(this, HighScoreActivity.class);
        startActivity(intent);
    }

    public void changeActivityToRawData()
    {
        Intent intent = new Intent(this, BrainwaveActivity.class);
        startActivity(intent);
    }

    public void StartTGDevice() {
        Button btnHighscore = (Button) findViewById(R.id.btnHighscore);
        btnHighscore.setEnabled(false);
        Button conBtn = (Button) findViewById(R.id.btnConnect);
        conBtn.setText("Connecting...");
        conBtn.setEnabled(false);
        AsyncConnector connect = new AsyncConnector(handler);
        connect.execute(this);
        findViewById(R.id.connectionWheel).setVisibility(View.VISIBLE);
    }

    public void DeviceConnected() {
        Button conBtn = (Button) findViewById(R.id.btnConnect);
        connected = true;
        conBtn.setText("Connected!");
        Button btnBrightPictureGame = (Button) findViewById(R.id.btnBrightPictureGame);
        Button btnCatScaleGame = (Button) findViewById(R.id.btnCatScaleGame);
        Button btnMedAttGame = (Button) findViewById(R.id.btnMedAttGame);
        Button btnTraficLight = (Button) findViewById(R.id.btnTraficLight);
        Button btnHighscore = (Button) findViewById(R.id.btnHighscore);
        Button btnRawData = (Button) findViewById(R.id.btnRawData);
        btnBrightPictureGame.setEnabled(true);
        btnCatScaleGame.setEnabled(true);
        btnHighscore.setEnabled(true);
        btnMedAttGame.setEnabled(true);
        btnTraficLight.setEnabled(true);
        btnRawData.setEnabled(true);
        findViewById(R.id.connectionWheel).setVisibility(View.INVISIBLE);
    }

    @Override
    public void Update(int attention, int meditation, int delta, int theta,
                       int lowAlpha, int highAlpha, int lowBeta, int highBeta,
                       int lowGamma, int midGamma) {

    }

    public TGHandler giveHandler()
    {
        return handler;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu (Menu menu) {
        if (!connected)
            menu.getItem(0).setEnabled(false);
        else
            menu.getItem(0).setEnabled(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            String state, version, address, name;

            try {
                state = String.valueOf(handler.DeviceInfo().getState());
                version = String.valueOf(handler.DeviceInfo().getVersion());
                address = handler.DeviceInfo().getConnectedDevice().getAddress();
                name = handler.DeviceInfo().getConnectedDevice().getName();
            }
            catch (Exception e) {
                state = "Error getting data from device";
                version = "Error getting data from device";
                address = "Error getting data from device";
                name = "Error getting data from device";
            }

            Intent intent = new Intent(this, SettingsActivity.class);
            intent.putExtra("State", state);
            intent.putExtra("Version", version);
            intent.putExtra("Address", address);
            intent.putExtra("Name", name);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
